﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{    
    public partial class YGRGS08Data : BaseFunction
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        /*public INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }

        SETINTEXDB INTEXDB = new SETINTEXDB();
         */
        public YGRGS08Data()
            : base()
        {
        }
         public YGRGS08Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }

        public DataSet GetGS07(DataRow dwTABEL)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "GS07";
            string COLOR = dwTABEL["COLOR"].ToString().Trim();
            string COUNT = dwTABEL["COUNT"].ToString().Trim();
            try
            {
                orlReadCommand = INTEXDBC.OracleDB.CreateCommand();
                orlReadCommand.CommandText = " SELECT DISTINCT B.gscart,C.GSHANDLNR,b.gscfarbe,b.GSCBEZ,b.gscmufrb, "
                    + " (SELECT STRAGG(REMARK) FROM ( "
                    + " select SUBSTR(TXTISIND,1,20) AS ART,SUBSTR(TXTISIND,24,10) AS COLOR, "
                    + " TRIM(TXTKTEXT)AS REMARK "
                    + " from txtind t1,TXTKON T2 "
                    + " where T1.TXTIMANDANT = T2.TXTKMANDANT AND T1.TXTIPIND = T2.TXTKPIND "
                    + " AND txtitxtart = 'GSC' ) X "
                    + " WHERE X.ART = A.GAGART  AND X.COLOR =A.GAGFARBE ) AS BEZ "
                    + " FROM GASTGA A,GSCOLOR B,GSDAT C "
                    + " WHERE A.GAGMANDANT = B.GSCMANDANT "
                    + " AND A.GAGART = B.GSCART AND A.GAGVAR = B.GSCVAR AND A.GAGFARBE = B.GSCFARBE "
                    + " AND A.GAGMANDANT = C.GSMANDANT AND A.GAGART = C.GSART "
                    + " AND A.GAGVAR = C.GSVAR "
                    + " AND B.gscmufrb LIKE '%"+COUNT+"%' "
                    + " AND (TRIM(A.GAGFARBE) = '"+COLOR+"' or B.GSCBEZ LIKE '%"+COLOR+"%') "
                    + " AND A.GAGART LIKE '3%' "
                    + " ORDER BY 1 "                   
                    ;

                Result = INTEXDBC.Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["BEZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["BEZ"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["GSHANDLNR"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["GSHANDLNR"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["GSCBEZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["GSCBEZ"] = value;
                    }
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet GetGS08(DataRow dwTABLE)
        {
            DataSet Result;
            DataSet Return = new DataSet();
            string TableName = "GS08";
            string ART = dwTABLE["GSCART"].ToString();
            string COLOR = dwTABLE["GSCFARBE"].ToString();
            try
            {
                orlReadCommand = INTEXDBC.OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT A.GAGPOS,SUBSTR(A.GAGFILLER,2,2) AS POS, "
                    + " (SELECT TRIM(SUBSTR(SDDATEN,2,40)) FROM SDDAT  "
                    + " WHERE SDSA = '0015' AND SUBSTR(SDKEY,3,2) = SUBSTR(A.GAGFILLER,2,2) "
                    + " AND SUBSTR(SDKEY,24,1)='V') AS TYPE, "
                    + " A.GAGGART,A.GAGGFARBE, "
                    + " (SELECT TRIM(SUBSTR(SDDATEN,2,40)) FROM SDDAT  "
                    + " WHERE SDSA = '0014' AND SUBSTR(SDKEY,3,10) = A.GAGGFARBE "
                    + " AND SUBSTR(SDKEY,24,1)='V') AS COLOR, "
                    + " to_char(A.GAGANTEIL * 0.001,'990.99')||'%' AS RATE,A.GAGABF*0.01||'%' AS COMSUM "
                    + " FROM GASTGA A "
                    + " WHERE A.GAGART = '"+ART+"' AND A.GAGFARBE = '"+COLOR+"' "
                    + " ORDER BY 1 "
                    ;
                Result = INTEXDBC.Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["TYPE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["TYPE"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["COLOR"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["COLOR"] = value;
                    }
                }
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet BuildData(DataRow dwUI)
        {
            /*DataSet SResult = new DataSet();
            SETINTEXDB SReturn = new SETINTEXDB(); */
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                setResult = GetGS07(dwUI);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }

    }
}